package com.hxkcloud.nurse.dborm;

import android.content.Context;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.misc.TransactionManager;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.table.TableUtils;
import com.hxkcloud.nurse.beans.MonitorKindBean;
import com.hxkcloud.nurse.beans.MonitorKindTestBean;
import com.hxkcloud.nurse.beans.MonitorKindValueBean;

import java.sql.SQLException;
import java.util.List;
import java.util.concurrent.Callable;

/**
 * Created by william_sun on 2016-07-22.
 * Description:患者监测类别
 */
public class MonitorKindDao {
    private Context context;
    private Dao<MonitorKindBean, Integer> monitorKindDao;
    private MonitorKindValueDao valueDao;
    private MonitorKindTestDao testDao;

    public MonitorKindDao(Context context) {
        this.context = context;
        try {
            monitorKindDao = DatabaseHelper.getHelper(context).getDao(MonitorKindBean.class);
            valueDao = new MonitorKindValueDao(context);
            testDao = new MonitorKindTestDao(context);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public List<MonitorKindBean> getKinds() {
        try {
            List<MonitorKindBean> kinds = monitorKindDao.queryForAll();
            for (MonitorKindBean kind : kinds) {
                kind.setValueList(valueDao.getValue(kind.getKindId()));
                kind.setTestList(testDao.getTest(kind.getKindId()));
            }
            return kinds;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public MonitorKindBean getKind(String kindId) {
        try {
            QueryBuilder builder = monitorKindDao.queryBuilder();
            builder.where().eq("kindId", kindId);
            List<MonitorKindBean> kinds = builder.query();
            MonitorKindBean kind = null;
            if (kinds != null && kinds.size() > 0) {
                kind = kinds.get(0);
                kind.setValueList(valueDao.getValue(kind.getKindId()));
                kind.setTestList(testDao.getTest(kind.getKindId()));
            }
            return kind;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void setKind(final List<MonitorKindBean> kinds, final String departmentId) {
        try {
            TransactionManager.callInTransaction(DatabaseHelper.getHelper(context).getConnectionSource(),
                    new Callable<Void>() {
                        @Override
                        public Void call() throws Exception {
                            clearKind();
                            for (MonitorKindBean kind : kinds) {
                                kind.setDepartmentId(departmentId);
                                monitorKindDao.create(kind);
                                if (kind.getValueList() != null && kind.getValueList().size() > 0) {
                                    for (MonitorKindValueBean value : kind.getValueList()) {
                                        value.setKindId(kind.getKindId());
                                        valueDao.setValue(value);
                                    }
                                }
                                if (kind.getTestList() != null && kind.getTestList().size() > 0) {
                                    for (MonitorKindTestBean test : kind.getTestList()) {
                                        test.setKindId(kind.getKindId());
                                        testDao.setTest(test);
                                    }
                                }
                            }
                            return null;
                        }
                    });
        } catch (SQLException e) {
        }
    }

    public void clearKind() {
        try {
            TableUtils.clearTable(monitorKindDao.getConnectionSource(), MonitorKindBean.class);
            valueDao.clearValue();
            testDao.clearTest();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public class MonitorKindValueDao {
        private Context context;
        private Dao<MonitorKindValueBean, Integer> monitorKindValueDao;

        public MonitorKindValueDao(Context context) {
            this.context = context;
            try {
                monitorKindValueDao = DatabaseHelper.getHelper(context).getDao(MonitorKindValueBean.class);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        public List<MonitorKindValueBean> getValue(String kId) {
            try {
                QueryBuilder builder = monitorKindValueDao.queryBuilder();
                builder.where().eq("kindId", kId);
                return builder.query();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return null;
        }

        public void setValue(List<MonitorKindValueBean> values) {
            try {
                for (MonitorKindValueBean v : values) {
                    monitorKindValueDao.create(v);
                }
            } catch (SQLException e) {
            }
        }

        public void setValue(MonitorKindValueBean value) {
            try {
                monitorKindValueDao.create(value);
            } catch (SQLException e) {
            }
        }

        public void clearValue() {
            try {
                TableUtils.clearTable(monitorKindValueDao.getConnectionSource(), MonitorKindValueBean.class);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public class MonitorKindTestDao {
        private Context context;
        private Dao<MonitorKindTestBean, Integer> monitorKindTestDao;

        public MonitorKindTestDao(Context context) {
            this.context = context;
            try {
                monitorKindTestDao = DatabaseHelper.getHelper(context).getDao(MonitorKindTestBean.class);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        public List<MonitorKindTestBean> getTest(String kId) {
            try {
                QueryBuilder builder = monitorKindTestDao.queryBuilder();
                builder.where().eq("kindId", kId);
                return builder.query();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return null;
        }

        public void setTest(MonitorKindTestBean test) {
            try {
                monitorKindTestDao.create(test);
            } catch (SQLException e) {
            }
        }

        public void setTest(List<MonitorKindTestBean> tests) {
            try {
                for (MonitorKindTestBean t : tests) {
                    monitorKindTestDao.create(t);
                }
            } catch (SQLException e) {
            }
        }

        public void clearTest() {
            try {
                TableUtils.clearTable(monitorKindTestDao.getConnectionSource(), MonitorKindTestBean.class);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
